﻿var __wf_sourceActionButton;

function checkFunction(funcationName) {
    var val = eval("typeof " + funcationName);
    return val == "function";
}

function invokeFunction(funcationName) {
    return eval(funcationName + "();");
}

function openWindowCenter(url, width, height) {
    var left = (screen.availWidth - width) / 2;
    var top = (screen.availHeight - height) / 2;

    var posArr = ["width=" + width, "height=" + height, "left=" + left, "top=" + top];
    var position = posArr.join(",");

    var sUrl = appendUrl(url, "rdm=" + Math.random());
    window.open(sUrl, "_blank", position + ",scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable=yes");
}

function appendUrl(url, paramPair) {
    var index = url.indexOf("?");
    if (index < 0) {
        return url + "?" + paramPair;
    }
    else {
        return url + "&" + paramPair;
    }
}

function validatePost() {
    var validateResult = true;
    if (event.srcElement.causesValidation == "True") {
        if ((typeof WebForm_PostBackOptions == "function") && (typeof WebForm_DoPostBackWithOptions == "function")) {
            var postBackOptions = new WebForm_PostBackOptions(event.srcElement.id, "", event.srcElement.causesValidation == "True", event.srcElement.validationGroup, '', false, false);
            WebForm_DoPostBackWithOptions(postBackOptions);
            if (typeof WebForm_OnSubmit == "function")
                validateResult = WebForm_OnSubmit();
        }
    }
    return validateResult;
}

function __wf_onActionClientClick() {
    if (!validatePost()) return false;

    var mask = document.getElementById('divMask');

    if (mask) {
        mask.className = 'mask';
        mask.style.height = document.documentElement.scrollHeight + "px";
    }

    var customResult = true;
    var customClientFunction = "__wf_onActionClientClick_Custom";
    if (checkFunction(customClientFunction))
        customResult = invokeFunction(customClientFunction);

    return customResult;
}

function __wf_ActionClientClickWithComments(url,isBaseUrl) {
    if (!validatePost()) return false;
    __wf_sourceActionButton = window.event.srcElement;
    var actionType = __wf_sourceActionButton.actionType;
    var sUrl;
    if (isBaseUrl) {
        sUrl = url + "/Workflow/Dialog/ExecuteWithComments.aspx?ActionType=" + actionType;
    }
    else {
        sUrl = url + "?ActionType=" + actionType;
    }

    openWindowCenter(sUrl, 500, 300);
}

function __wf_ActionClientClickWithComments_AskForMore(url, isBaseUrl, procInstId) {
    if (!validatePost()) return false;
    __wf_sourceActionButton = window.event.srcElement;
    var sUrl;
    if (isBaseUrl) {
        sUrl = url + "/Workflow/Dialog/AskForMore.aspx?ProcInstID=" + procInstId;
    }
    else {
        sUrl = url + "?ProcInstID=" + procInstId;
    }

    openWindowCenter(sUrl, 800, 500);
}

function __wf_ActionClientClickWithComments_Forward(url, isBaseUrl, procInstId, activityName) {
    if (!validatePost()) return false;
    __wf_sourceActionButton = window.event.srcElement;
    var sUrl;
    if (isBaseUrl) {
        var procInstIdPair = "ProcInstID=" + procInstId;
        var activityNamePair = "ActivityName=" + activityName;
        var paramArr = [procInstIdPair, activityNamePair]
        sUrl = url + "/Workflow/Dialog/Forward.aspx?" + paramArr.join("&");
    }
    else {
        sUrl = url + "?ProcInstID=" + procInstId;
    }

    openWindowCenter(sUrl, 800, 500);
}

function __wf_doActionPostBack(actionButton, auditDestination, comment) {
    if (auditDestination) {
        var auditDestinationControl = __wf_getAuditDestinationArgumentControl();
        auditDestinationControl.value = auditDestination;

        var auditDestinationAssignedControl = __wf_getAuditDestinationAssignedArgumentControl();
        auditDestinationAssignedControl.value = "1";
    }
    if (comment) {
        var commentControl = __wf_getCommentArgumentControl();
        commentControl.value = comment;
    }
    __doPostBack(actionButton.name, "");
}

function __wf_doActionAgentPostBack(actionType, auditDestination, comment) {
    if (actionType) {
        var actionTypeControl = __wf_getActionTypeArgumentControl();
        actionTypeControl.value = actionType;
    }
    if (auditDestination) {
        var auditDestinationControl = __wf_getAuditDestinationArgumentControl();
        auditDestinationControl.value = auditDestination;

        var auditDestinationAssignedControl = __wf_getAuditDestinationAssignedArgumentControl();
        auditDestinationAssignedControl.value = "1";
    }
    if (comment) {
        var commentControl = __wf_getCommentArgumentControl();
        commentControl.value = comment;
    }
    __doPostBack(__wf_getActionAgentButton().name, "");
}

function __wf_doActionClientClick_Log(url, isBaseUrl, procInstId) {
    var sUrl;
    if (isBaseUrl) {
        sUrl = url + "/Workflow/Dialog/Log.aspx?ProcInstID=" + procInstId;
    }
    else {
        sUrl = url + "?ProcInstID=" + procInstId;
    }

    openWindowCenter(sUrl, 800, 500);
}

function __wf_onActionClientClick_Close() {
    if (!validatePost()) return ;

    var customClientFunction = "__wf_onActionClientClick_Custom_Close";
    if (checkFunction(customClientFunction))
        invokeFunction(customClientFunction);
    else {
        window.opener = null;
        window.open('', '_self');
        window.close();
    }

    return;
}